﻿using System;
using System.Collections.Generic;
using YawetagDemo.Console.Models;
using YawetagDemo.Framework.Data;
using YawetagDemo.Framework.Services;
using YawetagLib.System.Data;
using YawetagLib.System.Facade.Services;
using YawetagLib.System.Services;

namespace YawetagDemo.Console.Services
{
    internal class Facade : BaseFacade
    {
        public Facade(BaseFacadeFactory facadeFactory, ServiceBroker serviceBroker)
            : base(facadeFactory, serviceBroker)
        {
        }

        public void DoBasicRetrieval()
        {
            System.Console.WriteLine("=== BASIC RETRIEVAL ===");

            System.Console.WriteLine("[...] Artist presentation list");
            IList<IArtist> artists = ServiceBroker.Get<IMusicService>().GetArtists();

            foreach (IArtist artist in artists)
            {
                System.Console.WriteLine(GetArtist(artist));
            }

            System.Console.WriteLine("[...] Press <ENTER> to continue");
            System.Console.ReadLine();
        }

        private static string GetArtist(IArtist artist)
        {
            ArtistModel model = new ArtistModel(artist);

            return string.Format("  {0} - Age: {1} - Bands: {2}", model.Name, model.Age, model.Bands);
        }

        public void DoCallbackOperations()
        {
            System.Console.WriteLine("=== CALLBACK OPERATIONS ===");

            System.Console.WriteLine("[...] Artist presentation list being logged back on the server side");
            ServiceBroker.Get<IMusicService>().LogArtists(GetArtist);

            System.Console.WriteLine("[...] Press <ENTER> to continue");
            System.Console.ReadLine();
        }

        public void DoComplexRetrieval()
        {
            System.Console.WriteLine("=== COMPLEX RETRIEVAL ===");

            IMusicService musicService = ServiceBroker.Get<IMusicService>();

            System.Console.WriteLine("[...] Top 10 first page");
            PrintAlbumRankingItems(musicService.GetAlbumRankingItems(string.Empty, string.Empty, 0, 5));

            System.Console.WriteLine("[...] Top 10 second page");
            PrintAlbumRankingItems(musicService.GetAlbumRankingItems(string.Empty, string.Empty, 5, 5));

            System.Console.WriteLine("[...] Pearl Jam's top 3");
            PrintAlbumRankingItems(musicService.GetAlbumRankingItems("Pearl Jam", string.Empty, 0, 3));

            System.Console.WriteLine("[...] Bottom 5");
            PrintAlbumRankingItems(musicService.GetAlbumRankingItems(string.Empty, "Stars asc", 0, 5));

            System.Console.WriteLine("[...] Press <ENTER> to continue");
            System.Console.ReadLine();
        }

        private static void PrintAlbumRankingItems(PagedResultInfo<IAlbumRankingItem> albumRankingsItems)
        {
            foreach (IAlbumRankingItem item in albumRankingsItems.ResultSet)
            {
                System.Console.WriteLine(GetAlbumRanking(item));
            }
        }

        private static string GetAlbumRanking(IAlbumRankingItem albumRanking)
        {
            AlbumRankingItemModel model = new AlbumRankingItemModel(albumRanking);

            return string.Format("  {0} - {1} - Released: {2} - Stars: {3}",
                model.BandName, model.Name, model.ReleaseDate, model.Stars);
        }

        public void DoCRUDOperations()
        {
            System.Console.WriteLine("=== CRUD OPERATIONS ===");

            ICoreService coreService = ServiceBroker.Get<ICoreService>();
            IMusicService musicService = ServiceBroker.Get<IMusicService>();

            System.Console.WriteLine("[...] Creating entities");
            IArtist lisa = musicService.CreateArtist("Lissa Gerrard", new DateTime(1900, 1, 1));
            IArtist brendan = musicService.CreateArtist("Brendan Perry", new DateTime(1959, 6, 30));
            IBand dcd = musicService.CreateBand("Dead Can Dancesss");
            musicService.AddArtistToBand(dcd.BandID, lisa.ArtistID);
            musicService.AddArtistToBand(dcd.BandID, brendan.ArtistID);

            IAlbum dcda = musicService.CreateAlbum(dcd.BandID, "Dead Can Dance", new DateTime(1984, 2, 27));
            IAlbum si = musicService.CreateAlbum(dcd.BandID, "Spleen and Ideal", new DateTime(1985, 11, 1));
            IAlbum wrds = musicService.CreateAlbum(dcd.BandID, "Within Realm Dying Sun", new DateTime(1987, 7, 27));
            IAlbum se = musicService.CreateAlbum(dcd.BandID, "The Serpent's Egg", new DateTime(1900, 1, 1));
            IAlbum aion = musicService.CreateAlbum(dcd.BandID, "Aion", new DateTime(1990, 6, 11));
            IAlbum il = musicService.CreateAlbum(dcd.BandID, "Into the Labyrinth", new DateTime(1993, 9, 13));
            IAlbum sc = musicService.CreateAlbum(dcd.BandID, "Spiritchaser", new DateTime(1996, 6, 3));

            IPerson jrgoe = coreService.GetPerson(1);
            IPerson sarofe = coreService.GetPerson(2);
            musicService.RankAlbum(dcda.AlbumID, jrgoe.PersonID, 3);
            musicService.RankAlbum(si.AlbumID, jrgoe.PersonID, 3);
            musicService.RankAlbum(wrds.AlbumID, jrgoe.PersonID, 4);
            musicService.RankAlbum(se.AlbumID, jrgoe.PersonID, 5);
            musicService.RankAlbum(aion.AlbumID, jrgoe.PersonID, 4);
            musicService.RankAlbum(il.AlbumID, jrgoe.PersonID, 4);
            musicService.RankAlbum(sc.AlbumID, jrgoe.PersonID, 4);
            musicService.RankAlbum(il.AlbumID, sarofe.PersonID, 5);
            musicService.RankAlbum(sc.AlbumID, sarofe.PersonID, 4);

            System.Console.WriteLine("[...] DCD's top 3");
            PrintAlbumRankingItems(musicService.GetAlbumRankingItems("Dead Can Dance", string.Empty, 0, 3));

            System.Console.WriteLine("[...] Updating entities");
            musicService.UpdateArtist(lisa.ArtistID, "Lisa Gerrard", new DateTime(1961, 4, 12));
            musicService.UpdateBand(dcd.BandID, "Dead Can Dance");
            musicService.UpdateAlbum(wrds.AlbumID, "Within the Realm of a Dying Sun", new DateTime(1987, 7, 27));
            musicService.UpdateAlbum(se.AlbumID, "The Serpent's Egg", new DateTime(1988, 10, 24));
            musicService.RankAlbum(wrds.AlbumID, jrgoe.PersonID, 5);
            musicService.RankAlbum(wrds.AlbumID, sarofe.PersonID, 5);
            musicService.RankAlbum(se.AlbumID, sarofe.PersonID, 5);
            musicService.RankAlbum(aion.AlbumID, sarofe.PersonID, 5);

            System.Console.WriteLine("[...] DCD's top 3 once again");
            PrintAlbumRankingItems(musicService.GetAlbumRankingItems("Dead Can Dance", string.Empty, 0, 3));

            System.Console.WriteLine("[...] Lisa and Brendan");
            lisa = musicService.GetArtist(lisa.ArtistID);
            brendan = musicService.GetArtist(brendan.ArtistID);
            System.Console.WriteLine(GetArtist(lisa));
            System.Console.WriteLine(GetArtist(brendan));

            System.Console.WriteLine("[...] Deleting entities");
            musicService.DeleteBand(dcd.BandID);
            musicService.DeleteArtist(lisa.ArtistID);
            musicService.DeleteArtist(brendan.ArtistID);

            System.Console.WriteLine("[...] Press <ENTER> to finish");
            System.Console.ReadLine();
        }
    }
}
